<?php

namespace App\Controller\System;

use App\Controller\ApiController;
use Usap\Service\Service;
use Think\Cache;


/**
 *
 * @desc 应用配置
 */
class MapInfoController extends ApiController
{

    public function getMapsInfo()
    {
        $user_id = $this->context->userId;
        \Logger::getLogger('home.user.account')->apps('login_start')->info(I());
        if (empty($user_id)) {
            $this->ajaxReturn(array('code' => -1001, 'code_str' => "请先登录"));
        }
        $device_service = Service::instance()->find('devices', 'devices');
        //获取设备类别配置
        $device_categroy_config = $device_service->getDeviceCategoryConfig();
        $where = 1;
        $type = I('request.type') ? I('request.type') : 1;
        if ($type == 2) { //烟感
            $where .= " and device_category ='11'";
        } elseif ($type == 3) {//温感
            $where .= " and device_category ='12'";
        } elseif ($type == 4) {//气感
            $where .= " and device_category ='10'";
        } elseif ($type == 5) { //警报
            $where .= " and is_alarming ='1'";
        }
        $device_list = $device_service->getDeviceList($where);
        $list = [];
        if ($device_list['list']) {
            $total = $yan = $qi = $wen = $alert = $online = $standby = 0;
            foreach ($device_list['list'] as $value) {
                $total++;
                if ($value['is_alarming'] == 1) {
                    $alert++;
                }
                if ($value['device_category'] == 10) {
                    $qi++;
                } elseif ($value['device_category'] == 11) {//
                    $yan++;
                } else {
                    $wen++;
                }
                if ($value['is_online'] == 1) {
                    $online++;
                }
                if ($value['is_standby'] == 1) {
                    $standby++;
                }
                $res['id'] = $value['device_id'];
                $res['codeserial'] = $value['codeserial'];
                $res['device_category'] = $device_categroy_config[$value['device_category']];
                $res['device_address'] = $value['device_address'];
                $res['is_standby'] = $value['is_standby'];
                $res['is_alarming'] = $value['is_alarming'];
                $res['is_onlie'] = $value['is_onlie'];
                $res['position'] = $value['position'] ? $value['position'] : '113.924535,22.571978';
                $list['device_list'][] = $res;
            }
            $list['device_summary'] = array(
                'total' => $total, 'yan' => $yan, 'qi' => $qi, 'wen' => $wen, 'alert' => $alert, 'standby' => $standby, 'online' => $online
            );
        }
        $return = array('code' => '0000', 'msg' => '获取数据成功', 'data' => $list);
        $this->ajaxReturn($return);
    }


    public function bigData()
    {
        $alarms_service = Service::instance()->find('common', 'alarms');
        $devices_service = Service::instance()->find('devices', 'devices');
        $date = date('Y-m-d');
        $files = "b.device_category,a.codeserial,a.status,a.alarm_type,a.type,FROM_UNIXTIME(a.created_time,'%Y-%m-%d %H:%i:%s') AS date,HOUR (FROM_UNIXTIME(a.created_time,'%Y-%m-%d %H:%i:%s')) AS hour,count(*) AS total";
        $where = "FROM_UNIXTIME(a.created_time,'%Y-%m-%d') = '$date') GROUP BY HOUR(FROM_UNIXTIME(a.created_time,'%Y-%m-%d %H:%i:%s')),a.alarm_type,a.type,a.status ORDER BY Hour(FROM_UNIXTIME(a.created_time,'%Y-%m-%d %H:%i:%s')";
        $info = $alarms_service->findAlamsInfo($where, $files);
        $ll = $dd = [];
        $r = $y = $w = 0;
        //1:火警、2:低电、3:漏电、4:烟雾、5:高温、6:消防栓开启、7:燃气
        $rhuojin = $rdidian = $rludian = $ryanwu = $rgaowen = $rxiaofang = $rranqi = 0;
        $yhuojin = $ydidian = $yludian = $yyanwu = $ygaowen = $yxiaofang = $yranqi = 0;
        $whuojin = $wdidian = $wludian = $wyanwu = $wgaowen = $wxiaofang = $wranqi = 0;
        for ($i = 1; $i <= 24; $i++) {
            $d ['时间'] = $i;
            $l ['时间'] = $i;
            if (empty($info)) {
                $d['报警次数'] = 0;
                $l['报警次数'] = 0;
            }
            foreach ($info as $value) {
                if ($value['hour'] == $i) {
                    $d['报警次数'] += $value['total'];
                    if ($value['status'] == 1) {
                        $l['报警次数'] += $value['total'];
                    } else {
                        $l['报警次数'] = 0;
                    }
                }
            }
            $dd[] = $d;
            $ll[] = $l;
        }
        foreach ($info as $value) {
            if ($value['device_category'] == 10) { //气感
                $r++;
                if ($value['type'] == 1) {
                    $rhuojin++;
                } elseif ($value['type'] == 2) {
                    $rdidian++;
                } elseif ($value['type'] == 3) {
                    $rludian++;
                } elseif ($value['type'] == 4) {
                    $ryanwu++;
                } elseif ($value['type'] == 5) {
                    $rgaowen++;
                } elseif ($value['type'] == 6) {
                    $rxiaofang++;
                } else {
                    $rranqi++;
                }
            } elseif ($value['device_category'] == 11) { //烟感
                $y++;
                if ($value['type'] == 1) {
                    $yhuojin++;
                } elseif ($value['type'] == 2) {
                    $ydidian++;
                } elseif ($value['type'] == 3) {
                    $yludian++;
                } elseif ($value['type'] == 4) {
                    $yyanwu++;
                } elseif ($value['type'] == 5) {
                    $ygaowen++;
                } elseif ($value['type'] == 6) {
                    $yxiaofang++;
                } else {
                    $yranqi++;
                }
            } else { //温感
                $w++;
                if ($value['type'] == 1) {
                    $whuojin++;
                } elseif ($value['type'] == 2) {
                    $wdidian++;
                } elseif ($value['type'] == 3) {
                    $wludian++;
                } elseif ($value['type'] == 4) {
                    $wyanwu++;
                } elseif ($value['type'] == 5) {
                    $wgaowen++;
                } elseif ($value['type'] == 6) {
                    $wxiaofang++;
                } else {
                    $wranqi++;
                }
            }
        }
        $condition = [];
        //设备列表
        $device_list = $devices_service->getDeviceListByFor($condition);
        $pos_arr = $cato = $arr = $cate_arr = [];
        foreach ($device_list as $vv) {
            $pvs = explode('-', $vv['district']);
            if (empty($pvs[1])) {
                $pvs[1] = '未知';
            }
            $arr[$pvs[1]][] = $vv;
            if (empty($vv['device_category'])) {
                $vv['device_category'] = 11;
            }
            $cate_arr[$vv['device_category']][] = $vv;//按类型分类
            if (empty($vv['position'])) {
                $vv['position'] = '114.085947,22.573257';
            }
            $position = explode(',', $vv['position']);
            $pp['lat'] = $position[0];
            $pp['lng'] = $position[1];
            $pos_arr[] = $pp;
        }
        $pps = [];//市级接入数组
        foreach ($arr as $key => $vvv) {
            $ps['地区'] = $key;
            $ps['数量'] = count($arr[$key]);
            $pps[] = $ps;
        }
        foreach ($cate_arr as $type => $cdd) {
            $online = 0;
            $total = count($cdd);
            foreach ($cdd as $cc) {
                if ($cc['is_online'] == 1) {
                    //在线
                    $online++;
                }
            }
            //在线率
            $cato[$type]['onlie_rate'] = round(($online / $total), 2);
        }
        $data = [
            'video' => [],
            'echart' => [
                // 今日报警情况
                [
                    'columns' => [
                        '时间', '报警次数'
                    ],
                    'rows' => $dd
                ],
                //报警类型占比 echart[1]
                [
                    'columns' => [
                        '类型',
                        '数量'
                    ],
                    'rows' => [
                        [
                            '类型' => '可燃气体探测器',
                            '数量' => $r,
                        ],
                        [
                            '类型' => '烟雾探测器',
                            '数量' => $y,
                        ],
                        [
                            '类型' => '温度探测器',
                            '数量' => $w,
                        ]
                    ]
                ],
                [
                    //今日处置警情情况 echart[2]
                    'columns' => ['时间', '报警次数'],
                    'rows' => $ll
                ],
                //区县接入排行 echart[3]
                [
                    'columns' => ['地区', '数量'],
                    'rows' => $pps
                ],
                //可燃气体探测报警原因统计 /*10.31更改*/ echart[4]
                [
                    'columns' => ['原因', '数量'],
                    'rows' => [
                        [
                            '原因' => '火警',
                            '数量' => $rhuojin
                        ],
                        [
                            '原因' => '低电',
                            '数量' => $rdidian
                        ],
                        [
                            '原因' => '漏电',
                            '数量' => $rludian
                        ],
                        [
                            '原因' => '烟雾',
                            '数量' => $ryanwu
                        ],
                        [
                            '原因' => '高温',
                            '数量' => $rgaowen
                        ],
                        [
                            '原因' => '消防栓开启',
                            '数量' => $rxiaofang
                        ],
                        [
                            '原因' => '燃气',
                            '数量' => $rranqi
                        ],
                    ]
                ],
                //温度探测报警原因统计 echart[5]
                [
                    'columns' => ['原因', '数量'],
                    'rows' => [
                        [
                            '原因' => '火警',
                            '数量' => $whuojin
                        ],
                        [
                            '原因' => '低电',
                            '数量' => $wdidian
                        ],
                        [
                            '原因' => '漏电',
                            '数量' => $wludian
                        ],
                        [
                            '原因' => '烟雾',
                            '数量' => $wyanwu
                        ],
                        [
                            '原因' => '高温',
                            '数量' => $wgaowen
                        ],
                        [
                            '原因' => '消防栓开启',
                            '数量' => $wxiaofang
                        ],
                        [
                            '原因' => '燃气',
                            '数量' => $wranqi
                        ],
                    ]
                ],
                //烟雾探测报警原因统计 echart[6]
                [
                    'columns' => ['原因', '数量'],
                    'rows' => [
                        [
                            '原因' => '火警',
                            '数量' => $yhuojin
                        ],
                        [
                            '原因' => '低电',
                            '数量' => $ydidian
                        ],
                        [
                            '原因' => '漏电',
                            '数量' => $yludian
                        ],
                        [
                            '原因' => '烟雾',
                            '数量' => $yyanwu
                        ],
                        [
                            '原因' => '高温',
                            '数量' => $ygaowen
                        ],
                        [
                            '原因' => '消防栓开启',
                            '数量' => $yxiaofang
                        ],
                        [
                            '原因' => '燃气',
                            '数量' => $yranqi
                        ],
                    ]
                ],
                //可燃气体在线率 echart[7]
                [
                    'columns' => ['type', 'value'],
                    'rows' => [
                        [
                            'type' => '在线率',
                            'value' => $cato[10]['onlie_rate']
                        ],
                    ]
                ],
                // 温度探测在线率 echart[8]
                [
                    'columns' => ['type', 'value'],
                    'rows' => [
                        [
                            'type' => '在线率',
                            'value' => $cato[11]['onlie_rate']
                        ],
                    ]
                ],
                // 烟雾探测在线率 echart[9]
                [
                    'columns' => ['type', 'value'],
                    'rows' => [
                        [
                            'type' => '在线率',
                            'value' => $cato[12]['onlie_rate']
                        ],
                    ]
                ],
                // 设备位置坐标 echart[10]
                [
                    'columns' => ['lat', 'lng'],
                    'rows' => $pos_arr
                ],
            ],
        ];
        $this->ajaxReturn(array('code' => '0', 'data' => $data));
    }

}
